package actions;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import beans.Usuarios;

import forms.loginForm;
import manager.UsuarioManager;


public class loginAction extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws  Exception {

		loginForm login = (loginForm) form;
		String forward = "error";
		
		if(login != null){
			Usuarios usuario = null;
			if(UsuarioManager.leerUsuarios(login) != null)
				usuario = (Usuarios) UsuarioManager.leerUsuarios(login).get(0);
			
			if(usuario != null){
				switch(usuario.getTipoUsuario()){
				case 1:
					forward = "admin";					
					break;
				case 2:
					forward = "cliente";
					break;
				case 3:
					forward = "empleado";
					break;
				}
				
				//Guardo algunos datos del usuario en la session
				Usuarios.guardarSession(request, usuario, usuario.getTipoUsuario());				
			}
		}
		return mapping.findForward(forward);
	}
}
